package sedev.shopping.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sedev.shopping.entities.DonViGia;
import sedev.shopping.entities.PhamVi;
import sedev.shopping.entities.ListPhamVi;
import sedev.shopping.util.Connector;
import sedev.shopping.util.HibernateUtil;
import sedev.shopping.util.Paging;

public class PhamViDAO {

	public PhamViDAO(){
		
	}
	
	private PhamVi initData(ResultSet result_set){
		PhamVi data = new PhamVi();
		try{
			data.setId(result_set.getInt(PhamVi.Properties.id.name()));
        }catch(SQLException sqle){
        	data.setId(0);
        }
        try{
        	data.setTen(result_set.getString(PhamVi.Properties.ten.name()));
        }catch(SQLException sqle){
        	data.setTen("");
        }        
		return data;
	}

	public boolean addData(PhamVi data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.save(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean updateData(PhamVi data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.update(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean removeData(Integer id){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;		
		try{
			transaction = session.beginTransaction();			
			PhamVi data = new PhamVi();
			data.setId(id);
			session.delete(data);
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}		
		return true;
	}
	
	public PhamVi getData(Integer id){
		PhamVi data = new PhamVi();		
		Session session = HibernateUtil.getSession().openSession();		
		try{
			session.load(data,id);		
		}catch(HibernateException ex){
			data = null;
		}
		return data;
	}
	
	public PhamVi getData(String condition){
		PhamVi data = new PhamVi();		
		Connector conn = Connector.getInstance();
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append("*");
		sql.append(" from ").append(PhamVi.Table);
		sql.append(" where ").append(condition);
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null && rs.next()){
            	data = this.initData(rs);                
            }
        }catch(SQLException sqle){
        	data = null;
        }
		return data;
	}
	
	public ListPhamVi getListData(String field, String condition, String order, Paging paging){
		ListPhamVi listdata = new ListPhamVi();			
		Connector conn = Connector.getInstance();
		
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append(field);
		sql.append(" from ").append(PhamVi.Table);
		sql.append(" where ").append(condition);
		if(order!=null && !order.equals("")){
			sql.append(" order by ").append(order);
		}
		if(paging.getPageSize() > 0){
			paging.setTotalRecord(conn.getTotalRecord(DonViGia.Table, condition));
            paging.checkPage();
            Long offset = paging.getCurrent().longValue() - 1;
            offset = offset * paging.getPageSize();
            sql.append(" limit ").append(offset).append(",").append(paging.getPageSize());
        }
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null){
                while(rs.next()){                   
                   listdata.add(this.initData(rs));
                }
            }
        }catch(SQLException sqle){
        	listdata.clear();
        }
		return listdata;
	}
}
